/* 引入基本模块 */
const [fs, ora, puppeteer, FakeUseragent, Message] = [
  require("fs"),
  require("ora"),
  require("puppeteer"),
  require("fake-useragent"),
  require("./console.color"),
]
const axios = require("axios")

async function init(id, prefix) {
  let total = 55,
    hash
  try {
    if (prefix) {
      const data = (await getBaseInfoById(id, prefix)).data
      total = data.total
      hash = data.hash
    } else {
      const {
        data: { photoList },
      } = (await getPhotoList(id)).data
      hash = photoList[0].photourl.replace(/\/0\.jpg$/, "")
    }
    const browser = await puppeteer.launch({
      headless: false,
    })
    const [page] = await browser.pages()
    await page.setViewport({
      width: 1500,
      height: 800,
    })
    let html = `<div style='
        display: grid; 
        gap: 10px; 
        grid-auto-rows: minmax(400px, 600px);
        grid-template-columns: repeat(4, minmax(300px, 500px));
      '>`
    for (let i = 1; i <= total; i++) {
      html += `<img style="width: 100%; height: 100%; object-fit: contain" src="${hash}/${i}.jpg" />`
    }
    page.setContent(html + "</div>", {
      timeout: 0,
      waitUntil: "networkidle2",
    })
  } catch (err) {
    console.log(err)
  }
}

/**
 * 根据图片id获取基本信息
 * @param {string}} id
 */
function getBaseInfoById(id, imgPrefix) {
  let data = fs.readFileSync("./allVipResponse.json", "utf8")
  let total = 55
  let target = JSON.parse(data).find(v => v.id === id)
  let hash = ""
  if (target) {
    hash = target.img.match(/(http|https).*(?=\/\w*\.jpg)/)[0]
  } else {
    hash = imgPrefix.match(/(http|https).*(?=\/\w*\.jpg)/)[0]
  }
  return Promise.resolve({ total, hash })
}

function getPhotoList(id) {
  return axios.post("http://inewgirl.com/api/embed/getPost", {
    id: id,
    p: 1,
    n: 100,
  })
}

let id = "36009"
let imgPrefix =
  "https://i.nshens.com/storage/image2/xiuren/no.4307/c0055d29584c0d42b5d43441ccecded6/1.jpg"
// init(id, imgPrefix)
init("29688")
